﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using IndoReputation.Models.BusinessObject;
using IndoReputation.Models.BusinessLogic;
using IndoReputation.Views.ViewModels;
using Telerik.Web.Mvc;

namespace IndoReputation.Controllers
{
    [Authorize]
    public class RegionController : BaseController
    {
        [HttpGet]
        public ActionResult Add()
        {
            var parent = RegionBOL.GetAllRegion();
            var listRegion = new List<Region>
                                 {
                                     new Region
                                         {
                                             ID = 0,
                                             Name = "No Parent"
                                         }
                                 };
            GetAllRegion(null, null, ref listRegion, parent);
            var model = new RegionViewModel
            {
                ListRegion = listRegion
            };
            return View(model);
        }

        [HttpGet]
        public ActionResult Edit(int regionID)
        {
            var parent = RegionBOL.GetAllRegion();
            var listRegion = new List<Region>
                             {
                                 new Region()
                                     {
                                         ID = 0,
                                         Name = "No Parent"
                                     }
                             };
            GetAllRegion(null, null, ref listRegion, parent);

            var region = RegionBOL.GetRegionById(regionID);

            var model = new RegionViewModel
            {
                CreateAt = region.CreateAt,
                CreateBy = region.CreateBy,
                Description = region.Description,
                ID = region.ID,
                Name = region.Name,
                RegionPID = region.RegionPID ?? 0,
                ListRegion = listRegion
            };
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(RegionViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.RegionPID == 0)
                    model.RegionPID = null;
                var reegion = new Region
                {
                    CreateAt = model.CreateAt,
                    CreateBy = model.CreateBy,
                    ID = model.ID,
                    Description = model.Description,
                    Name = model.Name,
                    RegionPID = model.RegionPID
                };
                RegionBOL.Update(reegion);
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Add(RegionViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.RegionPID == 0)
                    model.RegionPID = null;
                var newsRegion = new Region
                {
                    Description = model.Description,
                    Name = model.Name,
                    RegionPID = model.RegionPID
                };
                RegionBOL.Insert(newsRegion);
            }
            return RedirectToAction("Index");
        }        

        public ActionResult Index()
        {
            return View(RegionBOL.GetAllRegionJoin());
        }

        [GridAction]
        public ActionResult AjaxIndex()
        {
            return View(new GridModel(RegionBOL.GetAllRegionJoin()));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult AjaxDelete(int id)
        {
            //Find a customer with ProductID equal to the id action parameter            
            if (id > 0)
            {
                //Delete the record                
                if (!RegionBOL.Delete(id))
                {
                    ModelState.AddModelError(String.Empty, @"This Region have Country. Please check again!");
                }
            }

            //Rebind the grid
            return View(new GridModel(RegionBOL.GetAllRegion()));
        }

        public void GetAllRegion(int? parentId, Region parentNode, ref List<Region> afterBind, List<Region> source)
        {
            var filteredItems = source.Where(item => item.RegionPID == parentId);
            foreach (var i in filteredItems)
            {
                var temp = new Region
                {
                    ChildLocations = i.ChildLocations,
                    CreateAt = i.CreateAt,
                    CreateBy = i.CreateBy,
                    Description = i.Description,
                    ID = i.ID,
                    Name = i.Name,
                    RegionPID = i.RegionPID,
                    UpdateAt = i.UpdateAt,
                    UpdateBy = i.UpdateBy
                };
                if (parentNode == null)
                {
                    afterBind.Add(temp);
                }
                else
                {
                    parentNode.ChildLocations.Add(temp);
                }
                var n = temp;
                GetAllRegion(i.ID, n, ref afterBind, source);
            }
        }

    }
}
